package com.wonders.frame.controller;

import com.gcr.server.response.Response;
import com.wonders.frame.common.Enum.InitCommonSourceEnum;
import com.wonders.frame.common.Enum.SessionKeyEnum;
import com.wonders.frame.controller.core.CoreController;
import com.wonders.frame.entity.AuthAccount;
import com.wonders.frame.entity.AuthDicType;
import com.wonders.frame.entity.form.AuthDicTypeForm;
import com.wonders.frame.service.AuthDicService;
import com.wonders.frame.service.AuthDicTypeService;
import com.wonders.frame.service.Core.CoreCurdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/authDicType")
public class AuthDicTypeController extends CoreController<AuthDicType> {

    Logger log = LoggerFactory.getLogger(AuthDicTypeController.class);

    @Autowired
    private AuthDicTypeService authDicTypeService;
    @Autowired
    private AuthDicService authDicService;

    @Override
    protected CoreCurdService getService() {
        return authDicTypeService;
    }

    private final ReentrantLock lock=new ReentrantLock();

    /**
     * 加载树，所有字段类型
     *
     */
    @RequestMapping("getAuthDicTypeTreeNode")
    public Response getAuthDicTypeTreeNode(String type){
        try {
            return successResponse(authDicTypeService.getAuthDicTypeTreeNode(type));
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }
    }

    /**
     * 获得单节点数据
     */
    @RequestMapping("getOneAuthDicTypeNode")
    public Response getOneAuthDicTypeNode(String id){
        try {
            AuthDicType authDicType = authDicTypeService.getOneAuthDicTypeNode(id);
            return successResponse(authDicType);
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }
    }


    /**
     * 新增字典类型
     *
     */
    @RequestMapping("saveAuthDicType")
    public Response saveAuthDicType(@RequestBody AuthDicTypeForm authDicTypeForm){
        try {
            lock.lock();
            log.info("authDicType/saveAuthDicType");
            String id = authDicTypeForm.getId();
            String code = authDicTypeForm.getCode();
            if(authDicTypeService.checkUnique(id,code)){
                AuthDicType authDicType = authDicTypeService.saveAuthDicType(authDicTypeForm);
                InitCommonSourceEnum.INSTANCE.initDic();
                if(authDicType!=null){
                    return successResponse(authDicType);
                }else{
                    return errorResponse();
                }
            }else{
                return errorResponse(-2,"失败：代码重复",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }finally {
            lock.unlock();
        }

    }


    /**
     * 删除字典类型
     */
    @RequestMapping("/deleteAuthDicType")
    public Response deleteAuthDicType(String dicTypeId){
        try {
            log.info("authDicType/deleteAuthDicType");
            authDicTypeService.deleteAuthDicTypeById(dicTypeId);
            //authDicTypeService.deleteById(dicTypeId);
            InitCommonSourceEnum.INSTANCE.initDic();
            return successResponse(null);
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }
    }

    //模糊查询字典节点
    @RequestMapping("/findNodeByDicTypeName")
    public Response findNodeByOrgaName(String dicName, HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            AuthAccount acc =  (AuthAccount)session.getAttribute(SessionKeyEnum.USERINFO.name());
            return successResponse(authDicTypeService.findNodeByDicName(dicName));
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }
    }


}
